package com.study.web.controller.goal;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.study.app.service.IAppUserService;
import com.study.common.core.controller.AppBaseController;

import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.goal.domain.bo.GoalBo;
import com.study.goal.domain.vo.GoalVo;
import com.study.goal.service.IGoalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;


/**
 * 目标控制器
 *
 * @author MT
 * @date 2025-09-03
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/goals")
public class GoalController extends AppBaseController {

    private final IAppUserService iAppUserService;
    private final IGoalService iGoalService;


    /**
     * 分页查询学习目标列表,只获取父级的目标列表，子目标不提供，只提供数量
     *
     * @param pageQuery
     * @return 列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<GoalVo> list(PageQuery pageQuery) {
        return iGoalService.queryPageListById(getUserId(), pageQuery);
    }

    /**
     * 查询目标详情
     *
     * @param id 目标id
     * @return 目标详情
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<GoalVo> getInfo(@PathVariable("id") Long id) {
        return R.ok(iGoalService.queryById(id));
    }

    /**
     * 新增目标
     *
     * @param bo
     * @return 响应
     */
    @SaCheckLogin
    @PostMapping("/add")
    public R<Void> addGoal(@Validated @RequestBody GoalBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iGoalService.insertByBo(bo, bo.getParentId() != null));
    }

    /**
     * 修改目标
     *
     * @param bo
     * @return 响应
     */
    @SaCheckLogin
    @PostMapping("/edit")
    public R<Void> editGoal(@Validated @RequestBody GoalBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iGoalService.updateByBo(bo, bo.getParentId() != null));
    }
}
